<?php
class Users_model extends CI_Model {
	function validate(){
		$return = false;
		$pass = $this->input->post('password', true);
		$this->db->where('email', $this->input->post('email', true));
		$this->db->where('password', sha1(md5($pass)));
		$query = $this->db->get('mlme_users');
		if( $query->num_rows() == 1 ){
			$return = true;
		}
		return $return;
	}
	function create_users(){
		$params = array();
		$new_user = array(
			'firstname' => $this->input->post('reg-full-name'),
			'lastname' => $this->input->post('reg-last-name'),
			'middle' => $this->input->post('reg-middle-name'),
			'email' => $this->input->post('reg-email'),
			'password' => sha1(md5($this->input->post('reg-password'))),
			'type' => $this->input->post('type'),
			'status' => 0
		);
		$insert = $this->db->insert('mlme_users', $new_user);
		$id = $this->db->insert_id();
		$query = $this->db->query("select sha1(md5('$id')) as userid");
        $query_data= $query->row();
		$userid = $query_data->userid;
		$params['userid'] = $userid;
		$params['real_userid'] = $id;
		$params['insert'] = $insert;
		return $params;
	}

	function check_email()//check if email is exist already
	{
		$email = $this->input->post('reg-email');
		$query = $this->db->query("SELECT `email` FROM `mlme_users` WHERE `email` = '".$email."'");
		if( $query->num_rows() > 0 ){
			 return TRUE;
		}
		else {
		 	return FALSE;
		 	}
	}
	function get_user(){
		//$email = $this->input->post('email');
		$email = isset($_SESSION['email'])? $_SESSION['email']:'';//$this->session->userdata('email');
		$query = $this->db->query("SELECT * FROM `mlme_users` WHERE `email` = '".$email."'");
		//print_r($query->result());
		//$data = array();
		/*if ($query->num_rows() > 0)
		{

			foreach ($query->result() as $row)
			{
			   // echo $row->field;
			   $data[]=$row;

			}
		}*/
		return $query->row();
			//return $data;
	}
	function getFullname( $id=null ){
		if(empty($id)) return false;
		$query = 'SELECT `firstname`,`lastname`, `middle` FROM `mlme_users` WHERE `userid`='.$id.' LIMIT 1';
		return $this->db->query($query);
	}
	function verify_account($encrypted_id){
		$data = array("status"=>1,"date_updated"=>date('Y-m-d H:i:s'));
		$this->db->where("sha1(md5(userid))", $encrypted_id);
		$this->db->where("status", 0);
		$update = $this->db->update("mlme_users", $data);
		if($update){
			$query = $this->db->query("select email from mlme_users where sha1(md5(userid))='".$encrypted_id."' limit 1 ");
			if($query->num_rows() == 1){
				  $query_data = $query->row();
				  $_SESSION['email'] = $query_data->email;
				  $_SESSION['sess_con_msg'] = 1;//0 - do not display confirmation message 1 - display confirmation message
			}//end if
		}
		return ($update)? true:false;
	}
	function check_verified_account($email,$password){
		$this->db->where('email', $email);
		$this->db->where('password', sha1(md5($password)));
		$this->db->where('status',1);
		$query = $this->db->get('mlme_users');
		return ($query->num_rows() > 0)? true:false;
	}//end if
	function check_exist_email($email){
		$query = $this->db->query("SELECT email FROM mlme_users WHERE email = '".$email."'");
		return ($query->num_rows() > 0)? true:false;
	}//end fct
	function update_user_password($email,$new_pass){
		$qry = $this->db->query("select sha1(md5('$new_pass')) as new_pass");
        $qry_data = $qry->row();
        $new_pass = $qry_data->new_pass;
		$data = array('password' => $new_pass,'date_updated' => date('Y-m-d H:i:s'));
		$this->db->where('email',$email);
		$update = $this->db->update('mlme_users',$data);
		return ($update)? true:false;
	}//end if
	function edit_profile($post){
		$userid = Myuser_helper::get_userid($_SESSION['email']);
		/*$update_user = array(
			'firstname' => $this->input->post('fname'),
			'lastname' => $this->input->post('lname'),
		);*/
		$this->db->where('userid', $userid);
		$update = $this->db->update('mlme_users', $post);
		return $update;
	}
//	function update_pic(){
//		$userid = Myuser_helper::get_userid($_SESSION['email']);
//		$pic = array(
//               'picture' => $this->input->post('userpic'),
//            );
//		$this->db->where('userid', $userid);
//		$this->db->update('mlme_users', $pic);
//	}



}// end of Model